const puppeteer = require('puppeteer-core')
const config = require('config')

let browser = null

const generateBrowser = async () => {
  if (!browser) {
    browser = await puppeteer.launch(
      {
        args: ['--no-sandbox', '--disable-setuid-sandbox'],
        executablePath: config.get('puppeteer.executable-path')
      }
    )
    const page = await browser.newPage()
    await page.setDefaultNavigationTimeout(50000)
    await page.setCacheEnabled(true)
    return page
  } else {
    const page = await browser.newPage()
    await page.setDefaultNavigationTimeout(50000)
    await page.setCacheEnabled(true)
    return page
  }
}

module.exports = generateBrowser
